package com.tool.common.config;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * 数据库配置
 *
 * @author: zhoubin
 * @date: 2022/9/15
 */
public class DatabaseConfig {

    public static SqlSessionFactory sqlSessionFactory;

    static {
        initFactory();
    }

    private static void initFactory() {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        if(sqlSessionFactory == null) {
            initFactory();
        }
        return sqlSessionFactory;
    }
}
